16 Lecture

CS201

Midterm & Final Term Short Notes

Pointers (continued)

Pointers in programming are variables that store memory addresses, i.e., the location of other variables in memory. Pointers are used to manipulate and reference memory locations, and their manipulation is fundamental to programming in languages


Important Mcq's
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is the value of the pointer variable ptr after the following code is executed?

    arduino
    int *ptr; int x = 5; ptr = &x;

    a) 0 b) 5 c) Memory address of x d) Error

    Answer: c) Memory address of x

  2. Which operator is used to access the value at a memory location pointed to by a pointer? a) & b) * c) $ d) #

    Answer: b) *

  3. What is the purpose of a null pointer? a) It points to the memory location containing 0. b) It points to the memory location containing NULL. c) It is a pointer that has not been initialized. d) It is a pointer that points to the beginning of an array.

    Answer: b) It points to the memory location containing NULL.

  4. What is the output of the following code?

    perl
    int arr[3] = {1, 2, 3}; int *ptr = &arr[1]; printf("%d", *ptr);

    a) 1 b) 2 c) 3 d) Error

    Answer: b) 2

  5. What is the output of the following code?

    perl
    int arr[3] = {1, 2, 3}; int *ptr = arr; printf("%d", *(ptr + 2));

    a) 1 b) 2 c) 3 d) Error

    Answer: c) 3

  6. What is the difference between a pointer and a reference? a) A pointer is a variable that holds the memory address of another variable, while a reference is an alias for another variable. b) A pointer is a reference to another variable, while a reference is a variable that holds the memory address of another variable. c) There is no difference between a pointer and a reference. d) A pointer can be used with any data type, while a reference is limited to specific data types.

    Answer: a) A pointer is a variable that holds the memory address of another variable, while a reference is an alias for another variable.

  7. What is the purpose of the & operator in pointer operations? a) It returns the memory address of a variable. b) It dereferences a pointer variable. c) It assigns a value to a pointer variable. d) It tests a pointer for a null value.

    Answer: a) It returns the memory address of a variable.

  8. What is a dangling pointer? a) A pointer that points to a memory location that has been freed. b) A pointer that points to a null value. c) A pointer that has been assigned an incorrect memory address. d) A pointer that points to the beginning of an array.

    Answer: a) A pointer that points to a memory location that has been freed.

  9. What is the output of the following code?

    perl
    int arr[3] = {1, 2, 3}; int *ptr = arr; *ptr = 4; printf("%d", arr[0]);

    a) 1 b) 2 c) 3 d) 4

    Answer: d) 4

  10. What is the purpose of pointer arithmetic? a) To perform arithmetic operations on the memory locations pointed to by pointers. b) To perform arithmetic operations on the values



Subjective Short Notes
Midterm & Finalterm Prepration
Past papers included

Download PDF
  1. What is a pointer variable? Give an example of declaring a pointer variable in C. Answer: A pointer variable is a variable that stores the memory address of another variable. Example of declaring a pointer variable in C:
arduino
int *ptr;
  1. What is the difference between declaring a pointer variable and initializing a pointer variable? Answer: Declaring a pointer variable means specifying its type and name. Initializing a pointer variable means assigning a value (i.e., a memory address) to it.

  2. What is the difference between pointer arithmetic and normal arithmetic? Answer: Pointer arithmetic is performed on memory addresses, whereas normal arithmetic is performed on values. In pointer arithmetic, the value of a pointer is incremented or decremented based on the size of the data type that it points to.

  3. How do you allocate memory dynamically using the malloc() function in C? Answer: The malloc() function is used to dynamically allocate memory in C. It takes one argument, which is the size of the memory block to be allocated (in bytes). For example:

c
int *ptr = (int *) malloc(sizeof(int));
  1. What is a null pointer in C? Answer: A null pointer is a pointer that does not point to any memory address. In C, it is represented by the value 0 or NULL.

  2. What is a function pointer in C? Give an example of declaring a function pointer. Answer: A function pointer is a pointer that points to the memory address of a function. Example of declaring a function pointer:

c
int (*ptr)(int, int);
  1. What is the difference between a void pointer and a null pointer? Answer: A void pointer is a pointer that points to memory of any data type, whereas a null pointer is a pointer that does not point to any memory address.

  2. How do you use the "->" operator in C to access a member of a structure through a pointer? Answer: The "->" operator is used to access a member of a structure through a pointer. For example:

arduino
struct student { char name[50]; int age; }; struct student *ptr; ptr->age = 20;
  1. What is a double pointer in C? Give an example of declaring a double pointer. Answer: A double pointer is a pointer that points to the memory address of another pointer. Example of declaring a double pointer:
arduino
int **ptr;
  1. What is the difference between passing a variable by value and passing it by reference in a function call? Answer: When a variable is passed by value, a copy of its value is passed to the function, whereas when it is passed by reference, a reference to its memory address is passed to the function. This means that any changes made to the variable inside the function will affect the original variable when passed by reference, but not when passed by value.
In C programming, a pointer is a variable that stores the memory address of another variable. Pointers are powerful features that allow us to manipulate and work with data in a more efficient and flexible manner. Pointer arithmetic is an important aspect of working with pointers, allowing us to perform arithmetic operations directly on pointer variables. Dereferencing a pointer is also crucial, as it allows us to access the value that is stored in the memory location pointed to by the pointer. Pointers can be used in various ways, such as in dynamic memory allocation, passing arguments to functions, and manipulating complex data structures. It is important to properly initialize and free memory when using pointers to avoid memory leaks and other errors. In addition to single pointers, we can also work with double and triple pointers for more complex data structures. Pointers can also be used to create linked lists and other dynamic data structures. Overall, understanding pointers is crucial for any C programmer, as they provide a powerful tool for working with memory and data in a more efficient and flexible manner.